import React, { PropTypes } from 'react'
import { Form, Input, InputNumber, Radio, Modal } from 'antd'
const FormItem = Form.Item

const formItemLayout = {
  labelCol: {
    span: 6
  },
  wrapperCol: {
    span: 14
  }
};

const modal = ({
  visible,
  type,
  item,
  onOk,
  onCancel,
  form: {
    getFieldDecorator,
    validateFields,
    getFieldsValue
  }
}) => {
  function handleOk () {

    validateFields((errors) => {
      if (errors) {
        return
      }

      const createtime = getTime();

      function getTime(){

        const myDate = new Date;

        return ( myDate.getFullYear()
        + '-' + ((myDate.getMonth() + 1) < 10 ? '0' : '' )
        + (myDate.getMonth() + 1)
        + '-' + (myDate.getDate() < 10 ? '0' : '')
        + myDate.getDate()
        + ' ' + (myDate.getHours() < 10 ? '0' : '')
        + myDate.getHours()
        + ':' + (myDate.getMinutes() < 10 ? '0' : '')
        + myDate.getMinutes()
        + ':' + (myDate.getSeconds() < 10 ? '0' : '')
        + myDate.getSeconds());
      }

      const data = {
        ...getFieldsValue(),
        Createtime : createtime
      }
      onOk(data)
    })
  }

  console.log(item);

  const modalOpts = {
    title: `${type === 'create' ? '新建用户' : '修改用户'}`,
    visible,
    onOk: handleOk,
    onCancel,
    wrapClassName: 'vertical-center-modal'
  }

  return (
    <Modal {...modalOpts}>
      <Form horizontal>
        <FormItem label='姓名：' hasFeedback {...formItemLayout}>
          {getFieldDecorator('Username', {
            initialValue: item.Username,
            rules: [
              {
                required: true,
                message: '姓名未填写'
              }
            ]
          })(<Input />)}
        </FormItem>
        <FormItem label='密码：' hasFeedback {...formItemLayout}>
        {getFieldDecorator('Password', {
          initialValue: item.Password,
          rules: [
            {
              required: true,
              message: '密码未填写'
            }
          ]
        })(<Input />)}
        </FormItem>
        <FormItem label='权限：' hasFeedback {...formItemLayout}>
        {getFieldDecorator('Rules', {
          initialValue: item.Rules,
          rules: [
            {
              required: true,
              message: '权限未设置'
            }
          ]
        })(<Input />)}
        </FormItem>
        <FormItem label='状态：' hasFeedback {...formItemLayout}>
        {getFieldDecorator('Status', {
          initialValue: item.Status,
          rules: []
        })(<Input />)}
        </FormItem>
      </Form>
    </Modal>
  )
}

modal.propTypes = {
  form: PropTypes.object.isRequired,
  visible: PropTypes.bool,
  type: PropTypes.string,
  item: PropTypes.object
}

export default Form.create()(modal)
